package com.infra.server.codegenerator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import com.google.common.base.CaseFormat;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.util.StringUtils;

import java.util.Scanner;

/**
 * @author win
 * @date 2021/9/22 15:43
 * 代码生成器
 * https://github.com/baomidou/generator
 * 问题1：junit无法使用scanner键盘按键输入
 * -----解决：idea64.exe.vmoptions追加以下内容：-Deditable.java.test.console=true 然后重启IDEA
 */
@Slf4j
public class CodeGeneratorTest {

    @Test
    public void codeGeneratorMain() {
        new AutoGenerator(DATA_SOURCE_CONFIG)
                .global(GLOBAL_CONFIG)
                .packageInfo(PACKAGE_CONFIG)
                .template(TEMPLATE_CONFIG)
                .strategy(STRATEGY_CONFIG)
                .execute(new FreemarkerTemplateEngine());
    }

    /**
     * 读取控制台内容
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        log.info("请输入"+tip+":");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.hasText(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的"+tip+"!");
    }

    /**
     * 数据源配置
     */
    private static final DataSourceConfig DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://127.0.0.1:3306/infra?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8", "root", "root")
            .dbQuery(new MySqlQuery())
            .typeConvert(new MySqlTypeConvert())
            .keyWordsHandler(new MySqlKeyWordsHandler())
            .build();

    /**
     * 全局配置
     */
    private static final GlobalConfig GLOBAL_CONFIG = new GlobalConfig.Builder()
            .fileOverride()
            .openDir(false)
            .outputDir(System.getProperty("user.dir") + "/src/main/java/com/infra/server/codegenerator/")
            .author("zzd")
            .enableSwagger()
            .build();

    /**
     * 包配置
     */
    private static final PackageConfig PACKAGE_CONFIG = new PackageConfig.Builder()
            .parent("com.infra.server")
            .build();

    /**
     * 模板配置,不需要写后缀ftl
     */
    private static final TemplateConfig TEMPLATE_CONFIG = new TemplateConfig.Builder()
            .entity("/templates/entity.java")
            .service("/templates/service.java","/templates/serviceImpl.java")
            .mapper("/templates/mapper.java")
            .mapperXml("/templates/mapper.xml")
            .controller("/templates/controller.java")
            .build();

    /**
     * 策略配置
     */
    private static final StrategyConfig STRATEGY_CONFIG = new StrategyConfig.Builder()
            .addInclude(scanner("表名(多个表以逗号隔开)").split(","))
            .entityBuilder()
                .enableLombok()
                .columnNaming(NamingStrategy.underline_to_camel)
                .addTableFills(new Column("create_time", FieldFill.INSERT))
                .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
                .convertFileName(entityName -> CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,entityName))
                .enableSerialVersionUID()
            .controllerBuilder()
                .enableRestStyle()
                .convertFileName(entityName -> CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,entityName) + "Controller")
            .serviceBuilder()
                .convertServiceFileName(entityName -> CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,entityName) + "Service")
                .convertServiceImplFileName(entityName -> CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,entityName) + "ServiceImpl")
            .mapperBuilder()
                .convertMapperFileName(entityName -> CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,entityName) + "Mapper")
                .convertXmlFileName(entityName -> CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,entityName) + "Mapper")
            .build();

}
